﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HCC.Robotics.HCCRefereeModule
{
    public partial class RefereeBox : Form
    {
        FromWinformEvents _fromWinformPort;

        DateTime GameTime = new DateTime(0);
        DateTime RemainingTimeoutYellow;
        DateTime RemainingTimeoutBlue;
        int YellowTimeouts;
        int BlueTimeouts;
        int BlueScore = 0; int YellowScore = 0;
        private enum Stats
        {
            Waiting,
            KickoffFirstHalf,
            KickoffSecondHalf,
            FirstHalf,
            SecondHalf,
            Halftime,
            GameEnded
        }
        Stats CurrentStats = Stats.Waiting;
        private GameConfig _gameConfig;

        public RefereeBox(FromWinformEvents EventsPort, GameConfig gameConfig)
        {
            _fromWinformPort = EventsPort;
            _gameConfig = gameConfig;
            RemainingTimeoutYellow = new DateTime(2011, 1, 1, 0, _gameConfig.TimeoutLimit, 0);
            RemainingTimeoutBlue = new DateTime(2011, 1, 1, 0, _gameConfig.TimeoutLimit, 0);
            YellowTimeouts = BlueTimeouts = _gameConfig.NumberOfTimeouts;
            InitializeComponent();
            _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.Loaded, null, this));
            PlayTimer.Enabled = true;
            UIUpdateTimer.Enabled = true;
            // ID: 359
            _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.SetFirstHalf, null));
            // ID: 359
        }

        #region UI Updates
        private void PlayTimer_Tick(object sender, EventArgs e)
        {
            if (CurrentStats == Stats.FirstHalf)
            {
                GameTime = GameTime.AddSeconds(1);
                if (GameTime.Minute == _gameConfig.FirstHalfTime)
                {
                    string[] players = new string[4];
                    players[0] = "false";
                    players[1] = "false";
                    players[2] = "false";
                    players[3] = "false";
                    _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.ActivePlayers, players));
                    UpdateCurrent(Stats.Halftime);
                    return;
                }
            }
            if (CurrentStats == Stats.Halftime)
            {
                GameTime = GameTime.AddSeconds(1);
                if (GameTime.Minute == _gameConfig.HalfTime)
                {
                    UpdateCurrent(Stats.KickoffSecondHalf);
                    return;
                }
            }
            if (CurrentStats == Stats.SecondHalf)
            {
                GameTime = GameTime.AddSeconds(1);
                if (GameTime.Minute == _gameConfig.SecondHalfTime)
                {
                    UpdateCurrent(Stats.GameEnded);
                    return;
                }
            }
        }

        private void UIUpdateTimer_Tick(object sender, EventArgs e)
        {
            lblPlayTime.Text = string.Format("{0:D2}:{1:D2}", GameTime.Minute, GameTime.Second);
            lblRemainingTimeoutBlue.Text = string.Format("{0:D2}:{1:D2}", RemainingTimeoutBlue.Minute, RemainingTimeoutBlue.Second);
            lblRemainingTimeoutYellow.Text = string.Format("{0:D2}:{1:D2}", RemainingTimeoutYellow.Minute, RemainingTimeoutYellow.Second);
            lblTimeoutLeftBlue.Text = BlueTimeouts.ToString();
            lblTimeoutLeftYellow.Text = YellowTimeouts.ToString();
            lblBlueScore.Text = BlueScore.ToString();
            lblYellowScore.Text = YellowScore.ToString();
            if (CurrentStats == Stats.GameEnded)
            {
                if (BlueScore == YellowScore)
                {
                    // gelijk spel
                    lblGameStatus.Text = "We have a tie.";
                }
                else if (BlueScore > YellowScore)
                {
                    // blauw wint
                    lblGameStatus.Text = "The blue team wins.";
                }
                else
                {
                    // geel wint
                    lblGameStatus.Text = "The yellow team wins.";
                }
            }

            this.Invalidate();
        }

        public void SetStatus(string status)
        {
            lblStatus.Text = status;
            Console.Beep();

            if (status == "Ready for game ...")
            {
                UpdateCurrent(Stats.KickoffFirstHalf);
            }
            if (status == "Waiting for Teams ...")
            {
                lblGameStatus.Text = "Waiting for Teams ...";
            }
        }

        private void UpdateCurrent(Stats stats)
        {
            string[] players = new string[4];
            switch (stats)
            {
                case Stats.KickoffFirstHalf:
                    btnKickoffYellow.Enabled = true;
                    btnKickoffBlue.Enabled = false;
                    GameTime = new DateTime(0);
                    lblGameStatus.Text = "Ready to Kickoff the first half ...";
                    // ID: 359
                    _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.SetFirstHalf, null));
                    // ID: 359
                    break;
                case Stats.KickoffSecondHalf:
                    btnKickoffYellow.Enabled = false;
                    btnKickoffBlue.Enabled = true;
                    GameTime = new DateTime(0);
                    lblGameStatus.Text = "Ready to Kickoff the second half ...";
                    // ID: 359
                    _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.SetSecondHalf, null));
                    // ID: 359
                    break;
                case Stats.FirstHalf:
                    btnGoalBlue.Enabled = true;
                    btnGoalBlueMin.Enabled = true;
                    btnGoalYellow.Enabled = true;
                    btnGoalYellowMin.Enabled = true;
                    btnKickoffYellow.Enabled = false;
                    GameTime = new DateTime(0);
                    lblGameStatus.Text = "First half playing ...";
                    break;
                case Stats.SecondHalf:
                    btnGoalBlue.Enabled = true;
                    btnGoalBlueMin.Enabled = true;
                    btnGoalYellow.Enabled = true;
                    btnGoalYellowMin.Enabled = true;
                    btnKickoffBlue.Enabled = false;
                    GameTime = new DateTime(0);
                    lblGameStatus.Text = "Second half playing ...";
                    break;
                case Stats.Halftime:
                    btnGoalBlue.Enabled = false;
                    btnGoalBlueMin.Enabled = false;
                    btnGoalYellow.Enabled = false;
                    btnGoalYellowMin.Enabled = false;
                    btnKickoffBlue.Enabled = false;
                    btnKickoffBlue.Enabled = false;
                    GameTime = new DateTime(0);
                    lblGameStatus.Text = "Half time ...";
                    break;
                case Stats.GameEnded:
                    btnGoalBlue.Enabled = false;
                    btnGoalBlueMin.Enabled = false;
                    btnGoalYellow.Enabled = false;
                    btnGoalYellowMin.Enabled = false;
                    btnKickoffBlue.Enabled = false;
                    btnKickoffBlue.Enabled = false;
                    GameTime = new DateTime(0);
                    lblGameStatus.Text = "Game Ended ...";
                    players[0] = "false";
                    players[1] = "false";
                    players[2] = "false";
                    players[3] = "false";
                    _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.ActivePlayers, players));
                    break;
            }
            CurrentStats = stats;
        }
        #endregion

        #region GoalButton's

        private void btnGoalYellow_Click(object sender, EventArgs e)
        {
            YellowScore++;
            string[] players = new string[4];
            players[0] = "false";
            players[1] = "false";
            players[2] = "false";
            players[3] = "false";
            btnKickoffBlue.Enabled = true;
            _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.ActivePlayers, players));
            btnGoalBlue.Enabled = false;
            btnGoalYellow.Enabled = false;
            KickoffTimer.Enabled = false;
        }

        private void btnGoalYellowMin_Click(object sender, EventArgs e)
        {
            if (YellowScore > 0)
                YellowScore--;
        }

        private void btnGoalBlue_Click(object sender, EventArgs e)
        {
            BlueScore++;
            string[] players = new string[4];
            players[0] = "false";
            players[1] = "false";
            players[2] = "false";
            players[3] = "false";
            btnKickoffYellow.Enabled = true;
            _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.ActivePlayers, players));
            btnGoalBlue.Enabled = false;
            btnGoalYellow.Enabled = false;
            KickoffTimer.Enabled = false;
        }

        private void btnGoalBlueMin_Click(object sender, EventArgs e)
        {
            if (BlueScore > 0)
                BlueScore--;
        }

        #endregion

        #region Kickoff Buttons

        private void btnKickoffYellow_Click(object sender, EventArgs e)
        {
            string[] players = new string[4];
            players[0] = "true";
            players[1] = "true";
            players[2] = "false";
            players[3] = "false";
            _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.ActivePlayers, players));

            if (CurrentStats == Stats.KickoffFirstHalf)
            {
                UpdateCurrent(Stats.FirstHalf);
                lblStatus.Text = "";
                KickoffTimer.Enabled = true;
                return;
            }
            btnKickoffYellow.Enabled = false;
            btnGoalBlue.Enabled = true;
            btnGoalYellow.Enabled = true;
            KickoffTimer.Enabled = true;
        }

        private void btnKickoffBlue_Click(object sender, EventArgs e)
        {
            string[] players = new string[4];
            players[0] = "false";
            players[1] = "false";
            players[2] = "true";
            players[3] = "true";
            _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.ActivePlayers, players));

            if (CurrentStats == Stats.KickoffSecondHalf)
            {
                UpdateCurrent(Stats.SecondHalf);
                lblStatus.Text = "";
                KickoffTimer.Enabled = true;
                return;
            }
            btnKickoffBlue.Enabled = false;
            btnGoalBlue.Enabled = true;
            btnGoalYellow.Enabled = true;
            KickoffTimer.Enabled = true;
        }

        private void KickoffTimer_Tick(object sender, EventArgs e)
        {
            string[] players = new string[4];
            players[0] = "true";
            players[1] = "true";
            players[2] = "true";
            players[3] = "true";
            _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.ActivePlayers, players));
            KickoffTimer.Enabled = false;
        }

        #endregion

        private void btnShutdown_Click(object sender, EventArgs e)
        {
            _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.Exit, null));
            this.Close();
        }
    }
}
